/*************************************************************************
	> File Name: modbusClnt.c
	> Author:
	> Mail:
	> Created Time: 2017年03月16日 星期四 23时51分14秒
 ************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <time.h>

#include "modbusClnt.h"
#define BUFFSIZE 1024

pthread_mutex_t mutex;
struct timespec delay;

void sendMsg()
{
    while(1)
    {
        //pthread_mutex_lock(&mutex);
        if(pthread_mutex_trylock(&mutex)!=0)
        {
            perror("SendMsg Trylock Error:");
        }
        printf("sendMsgThread!!\n");
        sleep(1);
        pthread_mutex_unlock(&mutex);
    }
}

void receiveMsg(threadArgs *server_addr)
{
    while(1)
    {
        //pthread_mutex_lock(&mutex);
        char buf[BUFFSIZE]= {0};
        if(pthread_mutex_trylock(&mutex)!=0)
        {
            perror("ReceiveMsg Trylock Error:");
        }
        //printf("receiveMsgThread!!\n");
        int skfd=socket(AF_INET,SOCK_STREAM,0);
        if(skfd!=-1)
        {
            if(connect(skfd,(struct sockaddr *)(server_addr),sizeof(struct sockaddr))!=-1)
            {
                if(-1 == read(skfd,buf,BUFFSIZE))
                {
                    perror("Recv Error:");
                }
                else
                {
                    printf("Recv Msg:%s\n",buf);
                }
            }
        }
        sleep(1);
        pthread_mutex_unlock(&mutex);
    }
}

int main(int argc, char *argv[])
{
    int skfd;
    char buf[1024] = {0};
    struct sockaddr_in server_addr;
    struct hostent *host;
    int portnumber,nbytes;
    if(3 != argc || 0>(portnumber=atoi(argv[2])))
    {
        printf("Usage:%s hostname portnumber \n",argv[1]);
        exit(1);
    }
    if(NULL == (host=gethostbyname(argv[1])))
    {
        perror("Gethostname error:");
        exit(1);
    }

    /* 创建socket描述符 */
    //if(-1 == (skfd=socket(AF_INET,SOCK_STREAM,0)))
    //{
    //    perror("Socket Error:");
    //    exit(1);
    //}

    /* 客户端填充需要连接的服务器的地址信息结构体 */
    bzero(&server_addr,sizeof(server_addr));
    server_addr.sin_family=AF_INET;
    server_addr.sin_port=htons(portnumber);
    server_addr.sin_addr=*((struct in_addr *)host->h_addr);

    /* 客户端调用connect主动发起连接请求 */
    //if(-1 == connect(skfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr)))
    //{
    //    perror("Connect Error:");
    //    exit(1);
    //}

    pthread_t id1;
    pthread_t id2;
    pthread_mutex_init(&mutex,NULL);
    threadArgs arg1;
    arg1.skfd=skfd;
    arg1.length=1024;
    //arg1.buf={0};
    if(pthread_create(&id1,NULL,(void *)sendMsg,NULL)==-1)
    {
        perror("Create Pthread 1 Err:");
    }
    if(pthread_create(&id2,NULL,(void *)receiveMsg,&server_addr)==-1)
    {
        perror("Create Pthread 2 Err:");
    }
    pthread_join(id1,NULL);
    pthread_join(id2,NULL);
    pthread_mutex_destroy(&mutex);

    /*客户端只接收服务器发来的数据，然后就退出*/
    //if(-1 == read(skfd,buf,1024))
    //{
    //    perror("Recv Error:");
    //}
    //printf("Date arrived:%s",buf);

    /* 拆除TCP连接 */
    close(skfd);
    exit(0);
}
